package com.dpiinc.TechPage.user.utils;

import android.Manifest;
import android.content.Context;

import com.dpiinc.TechPage.R;

import com.dpiinc.TechPage.pen.dialog.ServiceHintDialog;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RequestExecutor;


import java.util.List;

/**
 * Created by hu489_000 on 2018-07-14.
 */

public class PermissionUtils {

    private static boolean showHintDialog = true;
    private boolean isHasAlwaysPermission = false;

    public static boolean checkWRITE_EXTERNAL_STORAGE(final Context context, final Action<List<String>> granted, final Action<List<String>> denied) {

        boolean allow_privacy = context.getSharedPreferences("app_language", Context.MODE_PRIVATE).getBoolean("allow_privacy", false);
        if(allow_privacy){
            if (AndPermission.hasPermissions(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                //执行业务
                return true;

            } else {

                //申请权限
                ServiceHintDialog.ShowDialog2(context, context.getResources().getString(R.string.service_dialog_title_hint),
                        context.getResources().getString(R.string.service_dialog_msg_hint1));

                showHintDialog = true;
                ServiceHintDialog.setShowHint(showHintDialog);

                AndPermission.with(context)
                        .runtime()
                        .permission(Manifest.permission.WRITE_EXTERNAL_STORAGE
//                                            , Manifest.permission.READ_PHONE_STATE
                        )
                        .rationale(new Rationale<List<String>>() {
                            @Override
                            public void showRationale(Context context, List<String> data, RequestExecutor executor) {
                                ServiceHintDialog.dismissDialog();
                                denied.onAction(data);
                                executor.execute();
                            }
                        })
                        .onGranted(new Action<List<String>>() {
                            @Override
                            public void onAction(List<String> data) {
                                ServiceHintDialog.dismissDialog();
                                granted.onAction(data);
                            }
                        })
                        .onDenied(new Action<List<String>>() {
                            @Override
                            public void onAction(List<String> data) {
                                ServiceHintDialog.dismissDialog();
                                showHintDialog = false;
                                ServiceHintDialog.setShowHint(showHintDialog);
                                //被拒绝，且选择了不再提醒之后，需要提示用户去设置页面手动打开
                                if (AndPermission.hasAlwaysDeniedPermission(context, data)) {
//                                ServiceDialog.ShowDialog(context, context.getResources().getString(R.string.storage_permission_application),
//                                        context.getResources().getString(R.string.need_storage_authority_real), new ServiceDialog.OnClickEditListener() {
//                                            @Override
//                                            public void onClickOK() {
//                                                ServiceDialog.dismissDialog();
//                                                AndPermission.with(context).runtime().setting().onComeback(new Setting.Action() {
//                                                    @Override
//                                                    public void onAction() {
//
//                                                    }
//                                                }).start();
//                                            }
//                                        });
                                } else {
                                    denied.onAction(data);
                                }
                            }
                        })
                        .start();

                return false;
            }
        }
        return false;
    }


}
